//
//  CHSBarButton.m
//  ChildHoodStemp
//
//  Created by love on 14-8-25.
//
//

#import "ISTThemeButton.h"

@interface ISTThemeButton ()
{
    NSString *_normalStr;
    NSString *_highlightedStr;
    NSString *_selectedStr;
    NSString *_disabledStr;
    
    NSString *_normalBackgroundStr;
    NSString *_highlightedBackgroundStr;
    NSString *_selectedBackgroundStr;
    NSString *_disabledBackgroundStr;
    
    NSString *_normalColorStr;
    NSString *_highlightedColorStr;
    NSString *_selectedColorStr;
}

@end

@implementation ISTThemeButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [CNMultiThemeManager registerForThemeChange:self];
        // Initialization code
    }
    return self;
}

- (void)setTitleColorStr:(NSString *)colorKey andDefaultColor:(UIColor *)color forState:(UIControlState)state{
    if (state == UIControlStateNormal) {
        _normalColorStr = colorKey;
    }else if (state == UIControlStateHighlighted){
        _highlightedColorStr = colorKey;
    }else if (state == UIControlStateSelected){
        _selectedColorStr = colorKey;
    }
    UIColor *newColor = [CNMultiThemeManager colorForKey:colorKey];
    [self setTitleColor:newColor?newColor:color forState:state];
}

- (void)setBackgroundImageStr:(NSString *)image forState:(UIControlState)state{
    UIImage *newImg = [CNMultiThemeManager imageForKey:image];
    if (state == UIControlStateNormal) {
        _normalBackgroundStr = image;
    }else if (state == UIControlStateHighlighted){
        _highlightedBackgroundStr = image;
    }else if (state == UIControlStateSelected){
        _selectedBackgroundStr = image;
    }else if (state == UIControlStateDisabled){
        _disabledBackgroundStr = image;
    }
    [self setBackgroundImage:newImg forState:state];
}

- (void)setImageStr:(NSString *)image forState:(UIControlState)state{
    UIImage *newImg = [CNMultiThemeManager imageForKey:image];
    if (state == UIControlStateNormal) {
        _normalStr = image;
    }else if (state == UIControlStateHighlighted){
        _highlightedStr = image;
    }else if (state == UIControlStateSelected){
        _selectedStr = image;
    }else if (state == UIControlStateDisabled){
        _disabledStr = image;
    }
    [self setImage:newImg forState:state];
}


- (void) applyTheme
{
    if (_normalStr && _normalStr.length) {
        [self setImage:[CNMultiThemeManager imageForKey:_normalStr] forState:UIControlStateNormal];
    }
    if (_highlightedStr && _highlightedStr.length) {
        [self setImage:[CNMultiThemeManager imageForKey:_highlightedStr] forState:UIControlStateHighlighted];
    }
    if (_selectedStr && _selectedStr.length) {
        [self setImage:[CNMultiThemeManager imageForKey:_selectedStr] forState:UIControlStateSelected];
    }
    
    if (_disabledStr && _disabledStr.length) {
        [self setImage:[CNMultiThemeManager imageForKey:_disabledStr] forState:UIControlStateDisabled];
    }
    
    if (_normalBackgroundStr && _normalBackgroundStr.length) {
        [self setBackgroundImage:[CNMultiThemeManager imageForKey:_normalBackgroundStr] forState:UIControlStateNormal];
    }
    if (_highlightedBackgroundStr && _highlightedBackgroundStr.length) {
        [self setBackgroundImage:[CNMultiThemeManager imageForKey:_highlightedBackgroundStr] forState:UIControlStateHighlighted];
    }
    if (_selectedBackgroundStr && _selectedBackgroundStr.length) {
        [self setBackgroundImage:[CNMultiThemeManager imageForKey:_selectedBackgroundStr] forState:UIControlStateSelected];
    }
    if (_disabledBackgroundStr && _disabledBackgroundStr.length) {
        [self setBackgroundImage:[CNMultiThemeManager imageForKey:_disabledBackgroundStr] forState:UIControlStateDisabled];
    }
    
    if (_normalColorStr && _normalColorStr.length && [CNMultiThemeManager colorForKey:_normalColorStr]) {
        [self setTitleColor:[CNMultiThemeManager colorForKey:_normalColorStr] forState:UIControlStateNormal];
    }
    if (_highlightedColorStr && _highlightedColorStr.length && [CNMultiThemeManager colorForKey:_highlightedColorStr]) {
        [self setTitleColor:[CNMultiThemeManager colorForKey:_highlightedColorStr] forState:UIControlStateHighlighted];
    }
    if (_selectedColorStr && _selectedColorStr.length && [CNMultiThemeManager colorForKey:_selectedColorStr]) {
        [self setTitleColor:[CNMultiThemeManager colorForKey:_selectedColorStr] forState:UIControlStateSelected];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
